var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test')
var db = mongoose.connection;
var Kitten;     // model

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
  // we're connected!
    console.log('hello');

    var kittySchema = mongoose.Schema({
        name: String
    });
    kittySchema.methods.speak = function(){
        var greeting = this.name?`my name is ${this.name}`:`meow!`;
        console.log(greeting);
    }

    Kitten = mongoose.model('Kitten', kittySchema);
    //A model is a class with which we construct documents. 
    //In this case, each document will be a kitten with properties and behaviors as declared in our schema
    
    // create_save();
    findAll();
    // query(function(params){
    //     console.log(params);
    // })


});

function create_save(){
    var silence = new Kitten({ name: 'ginger' });
    var fluffy = new Kitten({name: 'fluffy'});
    fluffy.speak();

    fluffy.save(function(err, fluffy){
        if(err) return console.log(err);
        fluffy.speak();
    })
}

function findAll(){
    Kitten.find(function(err, kittens){
        if(err) return console.log(err);
        console.log(kittens);
    })
}

function query(callback){
    Kitten.find({name: /fluff/}, callback);
}